package org.example;

import cn.hutool.core.io.FileUtil;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangjg
 * @desc c
 * @date 2023/04/13 14:11
 **/
public class OrderChecker {

    public static void main(String[] args) {
        String filePath = "D:\\logs\\mq\\";
        String file = filePath + "log4j2.log";
        List<String> list = FileUtil.readLines(file, "UTF-8");
//        segmentOrder(list);
        yangeOrder(list);
    }


    private static void clearFile(String filePath){
        List<String> paths = FileUtil.listFileNames(filePath);
        for(String path : paths){
            System.out.println(path);
            FileUtil.del(filePath+path);
        }
    }

    /**
     * 严格有序判断
     *
     * @param list
     */
    private static void yangeOrder(List<String> list) {
        HashMap<String, Integer> map = new HashMap<>();
        Integer lastValue = null;
        Integer curValue = null;
        AtomicInteger cnt = new AtomicInteger();
        for (String line : list) {
            if (!line.contains("queue_")) {
                continue;
            }
            String[] arr = line.split(" ");
            curValue = Integer.parseInt(arr[9]);
            if (lastValue != null) {
                if (curValue > lastValue) {
                    cnt.getAndIncrement();
                } else {
                    System.out.println("------------error cur=" + curValue + " last=" + lastValue);
                }
                lastValue = curValue;
            } else {
                lastValue = curValue;
                cnt.getAndIncrement();
            }
        }
        System.out.println("--------------end cnt=" + cnt.get());
    }

    /**
     * 部分有序判断
     *
     * @param list
     */
    private static void segmentOrder(List<String> list) {
        HashMap<String, Integer> map = new HashMap<>();
        AtomicInteger cnt = new AtomicInteger();
        for (String line : list) {
            if (!line.contains("queue_")) {
                continue;
            }
            String[] arr = line.split(" ");
            Integer value = map.get(arr[10]);
            String numStr = arr[9];
            if (value != null) {
                if (Integer.parseInt(numStr) > value) {
                    cnt.getAndIncrement();
                } else {
                    System.out.println("------------error cur=" + numStr + " last=" + value);
                }
            } else {
                cnt.getAndIncrement();
            }
            map.put(arr[10], Integer.parseInt(numStr));
        }
        System.out.println("--------------end cnt=" + cnt.get());
    }


}
